home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume6 / lowlav < prev    next >
Encoding:
Text File  |  1989-04-08  |  4.6 KB  |  182 lines

  1. Newsgroups: comp.sources.misc
  2. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  3. Subject: v06i084: lowlav -- return net host with lowest load average
  4. Reply-To: "John D. DiMarco" <jdd@db.toronto.edu>
  5.  
  6. Posting-number: Volume 6, Issue 84
  7. Submitted-by: "John D. DiMarco" <jdd@db.toronto.edu>
  8. Archive-name: lowlav
  9.  
  10. Description: lowlav: given a list of hosts, return the name of the host with the
  11.          lowest load average.
  12.  
  13. It's very simple, needs no makefile or readme. A man-page is included.
  14. It may be BSD - specific (does SYSV use /usr/spool/rwho/whod.* ?)
  15.  
  16. #! /bin/sh
  17. # This is a shell archive.  Remove anything before this line, then unpack
  18. # it by saving it into a file and typing "sh file".  To overwrite existing
  19. # If this archive is complete, you will see the following message at the end:
  20. #        "End of shell archive."
  21. #
  22. # Contents:
  23. #   lowlav.1 lowlav.c
  24. #
  25. # Wrapped by jdd@db.toronto.edu on Fri Mar 31 20:04:43 1989
  26. #
  27. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  28. if test -f lowlav.1 -a "${1}" != "-c" ; then 
  29.   echo shar: Will not over-write existing file \"lowlav.1\"
  30. else
  31. echo shar: Extracting \"lowlav.1\" \(600 characters\)
  32. sed "s/^X//" >lowlav.1 <<'END_OF_lowlav.1'
  33. X.TH LOWLAV 1V "31 March 1989"
  34. X.SH NAME
  35. Xlowlav \- print host with lowest load average
  36. X.SH SYNOPSIS
  37. X.B lowlav
  38. X[ -v ] [ -f hostfile ]
  39. X.SH DESCRIPTION
  40. X.I lowlav
  41. Xreads a list of hosts from the standard input, returns the host with the lowest 
  42. Xload average. 
  43. X.nf
  44. X.br
  45. X.SH OPTIONS
  46. X.IP \fB\-f\ \fIhostfile\fP
  47. XRead hosts from file "hostfile" instead of from the standard input.
  48. X.IP \fB\-v \fP
  49. XVerbose. Print an error message for every host not in /usr/spool/rwho.
  50. X.SH AUTHOR 
  51. XJohn DiMarco, University of Toronto, 1989
  52. X.SH FILES
  53. X/usr/spool/rwho/whod.*            Data Files
  54. X.SH SEE ALSO
  55. Xruptime(1C), rwho(1C), rwhod(8C)
  56. END_OF_lowlav.1
  57. if test 600 -ne `wc -c <lowlav.1`; then
  58.     echo shar: \"lowlav.1\" unpacked with wrong size!
  59. fi
  60. # end of overwriting check
  61. fi
  62. if test -f lowlav.c -a "${1}" != "-c" ; then 
  63.   echo shar: Will not over-write existing file \"lowlav.c\"
  64. else
  65. echo shar: Extracting \"lowlav.c\" \(2067 characters\)
  66. sed "s/^X//" >lowlav.c <<'END_OF_lowlav.c'
  67. X/*
  68. XGiven a list of machine names, return the name of the machine in that list
  69. Xwith the lowest load average in /usr/spool/rwho.
  70. X
  71. XWritten by John DiMarco, March 1989 at the University of Toronto
  72. XBased on a program 'asun' by Jonathon Haruni.
  73. X
  74. X */
  75. X
  76. X#include <stdio.h>
  77. X#include <string.h>
  78. X
  79. X#define STREQ(a,b)(strcmp((a),(b)) == 0)
  80. X#define TRUE  1
  81. X#define FALSE 0
  82. X
  83. X#define TOOBIGLOAD 30000
  84. X
  85. Xextern char *getenv();
  86. Xextern char *optarg;
  87. Xextern int opterr;
  88. X
  89. Xstruct partofrwhodfile {
  90. X    char fill[4];
  91. X    int garbage[2];
  92. X    char hostname[32];
  93. X    int loadav;
  94. X};
  95. X
  96. X
  97. Xmain(argc, argv)
  98. Xint argc;
  99. Xchar *argv[];
  100. X{
  101. X    FILE *fp, *input;
  102. X    int c, verbose, bestload = TOOBIGLOAD;
  103. X    char besthost[32];
  104. X    char thishost[80];
  105. X    char filename[120];
  106. X    struct partofrwhodfile entry;
  107. X
  108. X    *besthost = '\0';
  109. X
  110. X    verbose = FALSE;
  111. X    input = stdin;
  112. X
  113. X    while((c = getopt(argc, argv, "vf:")) != EOF){
  114. X        switch(c){
  115. X        case 'v':
  116. X            /* Verbose mode: print out error messages for      */
  117. X            /* hostnames which don't appear in /usr/spool/rwho */
  118. X            verbose = TRUE;
  119. X            break;
  120. X        case 'f':
  121. X            /* Take input from a file rather than stdin */
  122. X            input = fopen(optarg, "r");
  123. X            if(NULL==input){
  124. X                (void)fprintf(stderr,
  125. X                          "%s: Can't open file %s \n",
  126. X                          argv[0], optarg);
  127. X                (void)exit(1);
  128. X            }
  129. X            break;
  130. X        case '?':
  131. X            (void)fprintf(stderr, "Usage: %s [-v] [-f filename]\n",
  132. X                      argv[0]);
  133. X            (void)exit(1);
  134. X            break;
  135. X        }
  136. X
  137. X    }
  138. X    
  139. X    while(!feof(input)){
  140. X        (void)fscanf(input, "%80s ", thishost);
  141. X        (void)sprintf(filename,"/usr/spool/rwho/whod.%s",
  142. X                  thishost);
  143. X        if((fp = fopen(filename,"r")) != NULL) {
  144. X            (void)fread(&entry,sizeof(struct partofrwhodfile),
  145. X                  1,fp);
  146. X            (void)fclose(fp);
  147. X            if(entry.loadav < bestload) {
  148. X                (void)strcpy(besthost,thishost);
  149. X                bestload = entry.loadav;
  150. X            }
  151. X        } else {
  152. X            if(verbose){
  153. X                fprintf(stderr, "%s: %s not found\n", 
  154. X                    argv[0], filename);
  155. X            }
  156. X        }
  157. X    }
  158. X    if(TOOBIGLOAD == bestload){
  159. X        (void)fprintf(stderr, "%s: No data for specified hosts\n", 
  160. X                  argv[0]);
  161. X        (void)exit(1);
  162. X    }
  163. X    (void)printf("%s\n",besthost);
  164. X    if(input!=stdin)
  165. X        (void)fclose(input);
  166. X}
  167. END_OF_lowlav.c
  168. if test 2067 -ne `wc -c <lowlav.c`; then
  169.     echo shar: \"lowlav.c\" unpacked with wrong size!
  170. fi
  171. # end of overwriting check
  172. fi
  173. echo shar: End of shell archive.
  174. exit 0
  175.  
  176. ----
  177. John
  178.  
  179. John DiMarco    * We will live in the light *    jdd%db.toronto.edu@relay.cs.net
  180. jdd@db.toronto.edu     jdd@db.utoronto.ca    jdd@db.toronto.cdn
  181. {uunet!utai,watmath!utai,decvax!utcsri,decwrl!utcsri}!db!jdd    jdd@utcsri.UUCP
  182.